﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using DotnetSpider.DataFlow.Parser;
using DotnetSpider.DataFlow.Parser.Formatters;
using DotnetSpider.DataFlow.Storage;
using DotnetSpider.HBase;
using DotnetSpider.Http;
using DotnetSpider.Lawyee.Src;
using DotnetSpider.RabbitMQ;
using DotnetSpider.Sample.samples;
using DotnetSpider.Scheduler.Component;
using DotnetSpider.Selector;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Events;

namespace DotnetSpider.LawyeeUpdate
{
    class Program
    {
		static async Task Main(string[] args)
		{

			ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;

			System.Net.ServicePointManager.MaxServicePoints = 0;

			System.Net.ServicePointManager.MaxServicePointIdleTime = 1;


			Log.Logger = new LoggerConfiguration()
				.MinimumLevel.Information()
				.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
				.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
				.MinimumLevel.Override("System", LogEventLevel.Warning)
				.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Warning)
				.Enrich.FromLogContext()
				.WriteTo.Console().WriteTo.RollingFile("logs/spider.txt")
				.CreateLogger();

			try
			{

				DateTime dt = Convert.ToDateTime("1985-01-01");
				for (int i = 0; i < 12954; i++)
				{
					DateTime d = dt.AddDays(i);
					int count = 0;
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate1"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] = @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate2"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate3"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate4"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate5"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate6"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate7"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate8"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate9"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate10"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateA"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateB"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateC"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateD"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateE"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateF"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateG"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateH"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateI"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateJ"]))
					{
						count += Convert.ToInt32(conn.ExecuteScalar($"select COUNT(1)  from record where  [record].[current] =  @dateone", new { dateone = d.ToString("yyyy-MM-dd") }));
					}


					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDateA"]))
					{
						conn.ExecuteScalar($"UPDATE dateOne SET completed = @completed where  dateone = @dateone", new { dateone = d.ToString("yyyy-MM-dd"), completed = count });
					}
				}

			}
			catch (Exception ex)
			{
				Log.Logger.Error(ex.ToString());
			}

			Console.WriteLine("Bye!");
			Environment.Exit(0);
		}
	}
}
